/**
 * Created by cchen on 2015/4/28.
 */

var serviceAsUser = require('../../services/backoffice/backOffice-serviceAsUserService');
var messageUtil = require('../../common/utils/messageUtil');
var toolUtil = require('../../common/utils/toolUtil');




onError=function(msg,err, req,res){
    logger.error('Service As User controller '+msg+'  - - ' + err.message?err.message:'error');
    res.json(messageUtil.composeFailedMessage(req, err.message));
}

onSuccess=function(data,req,res)
{
    res.json(messageUtil.composeSucceededMessage(req, null, data));
}

onResponse=function(err,data,req,res){
    if(err){
        onError(err.message,err,req,res);
    }else {
        onSuccess(data, req, res);
    }
}

exports.createUserForService=function(req,res)
{
    var serviceId=req.params.serviceId
    var callback=function(err,result)
    {
        if(err)
        {
            onError("createUserForService",err,req,res);

        }
        console.log(serviceId);
        serviceAsUser.chainUpServicAndUser(serviceId,result._id,function(err,result1){
            onResponse(err,result,req,res);
        })
    }
    serviceAsUser.createServiceUser(callback);

}

exports.createServiceUserChain=function(req,res)
{
    var serviceId=res.params.serviceId
    var userId=res.params.userId
    serviceAsUser.chainUpServicAndUser(serviceId,userId,function(err,result){
        onResponse(err,result,req,res);
    })

}

exports.removeServiceUserChain=function(req,res)
{
    var serviceId=req.params.serviceId;
    serviceAsUser.removeServiceChain(serviceId,function(err,result){
        onResponse(err,result,req,res);
    })
}

exports.availableCommunities=function(req,res){
    serviceAsUser.getAvailableCommunities(function(err,result){
        onResponse(err,result,req,res);

    })
}

exports.getAllUnbindServiceUser=function(req,res)
{
    serviceAsUser.getAllUnbindServiceUser(function(err,result){
        onResponse(err,result,req,res);
    })
}

exports.getBindedServiceUser=function(req,res)
{
    var serviceId=req.params.serviceId;
    serviceAsUser.getBindedServiceUser(serviceId,function(err,result){
        onResponse(err,result,req,res);
    })
}

exports.updateSignedService=function(req,res){
    var body=req.body;
    serviceAsUser.updateSignedService(body.list,function(err,result){
        onResponse(null,result,req,res);
    })

}

